package com.pb.camera.fragment;

import java.util.ArrayList;
import java.util.List;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.pb.camera.R;
import com.pb.camera.adapter.CameraPictureListViewAdapter;
import com.pb.camera.application.App;
import com.pb.camera.bean.Camera;
import com.pb.camera.bean.Equipment;
import com.pb.camera.bean.Room;
import com.pb.camera.utils.JsonAnalyle;
import com.pb.camera.utils.Url;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class HomePageViewPagerFragment extends Fragment {
	private  ListView  cameraPictureListView;
	private  CameraPictureListViewAdapter  adapter;
	private   Room  rooms;
	/**
	 * 表示自己在viewpager的位置
	 */
	private   int  position;
	
	private   List<Equipment>  el;
	public int getPosition() {
		return position;
	}
	public void setPosition(int position) {
		this.position = position;
	}
	private  HttpUtils  httpsUtils=new HttpUtils();
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	el=new ArrayList<Equipment>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	View view =inflater.inflate(R.layout.camera_listview, container,false);
    	initView(view);
    	//获取房间内部的所有的设备
    	getAllCameraFrionNet(0, rooms);
    	return super.onCreateView(inflater, container, savedInstanceState);
    }
    
    private  void initView(View view)
    {       	
    	cameraPictureListView=(ListView) view.findViewById(R.id.camera_list);
    //	cameraPictureListView.setAdapter(adapter);
    	
    }
    
    public void   seCamerasAndRooms( Room rooms)
    {
    	this.rooms=rooms;
    }
    
    
	/**
	 * 从网络上面获取房间内的全部摄像头设备
	 * 获取的只是指定房间的设备,不是所有的房间的设备
	 */
	private void getAllCameraFrionNet(int positon,final Room  room) {
		if (room== null)
			return;
      String   path=Url.requestGroupEquipment(room.getGids(), App.getInstance().getUserId());
      httpsUtils.send(HttpMethod.POST, path, new RequestCallBack<String>() {    	  
		@Override
		public void onFailure(HttpException arg0, String arg1) {
			// TODO Auto-generated method stub
			Toast.makeText(getActivity(), "网络访问失败,请检查你的网络", Toast.LENGTH_SHORT);
		}

		@Override
		public void onSuccess(ResponseInfo<String> arg0) {
			// TODO Auto-generated method stub
			    boolean flag= JsonAnalyle.jsonAnalyse(arg0.result, getActivity(), null, null);
			    if (flag) {
					final List<Equipment>  dataList=JsonAnalyle.getEquipment(arg0.result);
					for (int i = 0; i < dataList.size(); i++) {
						//设置房间号
					dataList.get(i).setGroup(room.getGids()+"");
					}
					try {
						getActivity().runOnUiThread(new Runnable() {						
							@Override
							public void run() {
								// TODO Auto-generated method stub
								sortDeviceByType(dataList);
								adapter.resetEquipmentList(el);
								cameraPictureListView.setAdapter(adapter);
							//	adapter.notifyDataSetChanged();
							}
						});
					} catch (Exception e) {
						// TODO: handle exception
					}
					
				}
		}
	});      
	}	

	
	/**
	 * 过滤获取的所有设备,只保留里面的摄像头设备
	 */
	private   void   sortDeviceByType( List<Equipment>  dataList)
	{
		 if (dataList!=null&&dataList.size()>0) {
			for (Equipment e:dataList) {
				int type=e.getDtypes();
				if (type==5) {
					el.add(e);
				}
			}
		}
	}
}
